package com.shoplink.view;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.NinePatchDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.RelativeLayout;

public class ScaleView extends RelativeLayout implements OnClickListener {
	private static final String TAG = "ScaleViewPanel";

	private Animation mEnlarger;

	private Animation mLessen;

	private boolean mInitial;

	private OnClickListener mOnClickListener;

	/**
	 * The drawable used to draw the selector
	 */
	NinePatchDrawable mSelector;

	/**
	 * Defines the selector's location and dimension at drawing time
	 */
	Rect mSelectorRect = new Rect();

	private OnFocusChangeListener changeListener;

	public ScaleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.d(TAG, "ScaleViewPanel constructor");
		// setClipToPadding(false);
		setFocusableInTouchMode(true);
		setClipChildren(false);
		this.setClickable(true);
		this.setOnClickListener(this);
		setFocusable(true);
		// mSelector = (NinePatchDrawable) context.getResources().getDrawable(
		// R.drawable.focus);
		// TODO Auto-generated constructor stub
	}

	public void setOnfocusListen(OnFocusChangeListener changeListener) {
		this.changeListener = changeListener;

	}

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		super.onLayout(changed, l, t, r, b);

		if (!mInitial) {
			mInitial = true;

			mEnlarger = getScaleAnim(true);
			mLessen = getScaleAnim(false);
		}
	}

	@Override
	protected void onFinishInflate() {
	}

	private Animation getScaleAnim(final boolean enlarger) {
		int width = getMeasuredWidth();
		int height = getMeasuredHeight();

		int interval = Math.round(15);
		int duration;

		float fromX, toX, fromY, toY;
		if (enlarger) {
			fromX = 1.0f;
			fromY = 1.0f;

			toX = (float) (width + interval * 1.3f) / width;
			toY = (float) (height + interval * 1.3f) / height;

			duration = 300;
		} else {
			fromX = (float) (width + interval * 1.3f) / width;
			fromY = (float) (height + interval * 1.3f) / height;

			toX = 1.0f;
			toY = 1.0f;

			duration = 200;
		}

		Animation anim = new ScaleAnimation(fromX, toX, fromY, toY,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);

		anim.setFillAfter(true);
		anim.setDuration(duration);
		anim.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationRepeat(Animation animation) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationEnd(Animation animation) {

			}
		});
		return anim;
	}

	public void executeTo(boolean enlarger) {
		if (!enlarger) {
			mEnlarger.cancel();
		}
		if (mEnlarger == null || mLessen == null) {
			mEnlarger = getScaleAnim(true);
			mLessen = getScaleAnim(false);
		}
		startAnimation(enlarger ? mEnlarger : mLessen);
	}

	// public Bitmap getSmallBitmap(Bitmap bm, int w, int h) {
	// int width = bm.getWidth();
	// int height = bm.getHeight();
	// int newWidth = w;
	// int newHeight = h;
	// float scaleWidth = ((float) newWidth) / width;
	// float scaleHeight = ((float) newHeight) / height;
	// Matrix matrix = new Matrix();
	// matrix.postScale(scaleWidth, scaleHeight);
	// return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
	// }

	public void setClickListener(OnClickListener clickListener) {
		mOnClickListener = clickListener;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (mOnClickListener != null) {
			mOnClickListener.onClick(this);
		}
	}

	@Override
	protected void onFocusChanged(boolean gainFocus, int direction,
			Rect previouslyFocusedRect) {
		/*
		 * The gallery shows focus by focusing the selected item. So, give focus
		 * to our selected item instead. We steal keys from our selected item
		 * elsewhere.
		 */
		executeTo(gainFocus);
		this.bringToFront();
		if (changeListener != null) {
			changeListener.onFocusChange(ScaleView.this, gainFocus);
		}
		doAnim(this.getChildAt(0), this.getWidth(), this.getHeight(), gainFocus);

		super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);

	}

	private int alpValue = 0;

	@Override
	protected void dispatchDraw(Canvas canvas) {
		super.dispatchDraw(canvas);
		// final int adjust = 0;
		// int paddingBottom = getPaddingBottom();
		// Log.d(TAG, "paddingBottom = :" + paddingBottom);
		//
		// if (hasFocus() && mSelector != null) {
		// Rect padding = new Rect();
		// mSelector.getPadding(padding);
		// mSelector.setAlpha(alpValue);
		// Rect focusRect = new Rect(-padding.left + adjust, -padding.top
		// + adjust, getWidth() + padding.right - adjust, getHeight()
		// + padding.bottom - adjust - paddingBottom);
		// mSelectorRect.set(focusRect);
		// mSelector.setBounds(mSelectorRect);
		// mSelector.draw(canvas);
		// }

	}

	AnimatorSet set;

	public void doAnim(View fView, int w, int h, boolean isCom) {

		if (set == null) {
			set = new AnimatorSet();
		} else {
			set.end();
		}
		if (isCom) {
			ObjectAnimator animatorH = ObjectAnimator.ofInt(fView, "h",
					fView.getHeight(), h);
			ObjectAnimator animatorW = ObjectAnimator.ofInt(fView, "w",
					fView.getWidth(), w);
			set.playTogether(animatorW, animatorH);
			set.setInterpolator(new AccelerateDecelerateInterpolator());
			set.setDuration(300);
			set.start();
		} else {
			ObjectAnimator animatorH = ObjectAnimator.ofInt(fView, "h",
					fView.getHeight(), 0);
			ObjectAnimator animatorW = ObjectAnimator.ofInt(fView, "w",
					fView.getWidth(), 0);
			set.playTogether(animatorW, animatorH);
			set.setInterpolator(new AccelerateDecelerateInterpolator());
			set.setDuration(300);
			set.start();
		}
	}

}
